<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018
// +----------------------------------------------------------------------
// | API接口
// +---------------------------------------------------------------------
// | Author: lonjie <909241102@qq.com>
// +----------------------------------------------------------------------
namespace cmf\controller;

use think\Db;
use app\admin\model\ThemeModel;
use think\View;

class ApiController extends BaseController
{
    function _initialize(){
        //$this->limit();
    }

   function json($data=[], $success=true, $msg='success', $code=200){
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, WsdAuthToken");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        $result = [
            'data' => $data,
            'success' => $success,
            'msg' => $msg,
            'code' => $code,
            // 'session_id' => session_id()
        ];
        return json_encode($result);
    }

    function errorjson($msg='', $code=200){
        return $this->json(null, false, $msg, $code);
    }

    function successjson($msg='', $code=200){
        return $this->json(null, true, $msg, $code);
    }

    /**
     * 获取body的参数
     * @param param string 单个单数名
     * @param value string 默认值
     * @return mixed
     */
    function data($param='', $value=''){
        $str = file_get_contents('php://input');
        $data = json_decode($str, true);
        if ($param) {
            return $data[$param]?$data[$param]:$value;
        }
        return $data;
    }

}